// pages/order/orderConfirm/orderConfirm.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        totalPrice: 0,
        servicePrice: 0,
        actualPrice: 0,
        postage: 0,
        orderMark: '',
        cartSelectList: [],
        cartIdList: '',
        address: null,
        payMethods: 1,
        isVipGoods: null,
        isLoading: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getOrderInfo()
        this.getAddress()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    onUnload: function() {
        wx.removeStorageSync('selectAddress')
    },

    getOrderInfo: function() {
        this.setData({
            totalPrice: wx.getStorageSync('totalPrice'),
            actualPrice: wx.getStorageSync('actualPrice'),
            cartSelectList: wx.getStorageSync('cartSelectList'),
            cartIdList: wx.getStorageSync('cartIdList'),
            isVipGoods: wx.getStorageSync('isVipGoods')
        })
    },

    getAddress() {
        const that = this
        if (wx.getStorageSync('selectAddress')) {
            let address = wx.getStorageSync('selectAddress')
            that.setData({
                address: address
            })
            if (wx.getStorageSync('isVipGoods') == 1) {
                that.setData({
                    postage: address.extra_money_goods
                })
            } else {
                that.setData({
                    postage: address.extra_money_vip
                })
            }
        } else {
            let data = {}
            common.post('Address/address_list', data).then(function(res) {
                if (res.code == 204) {
                    wx.showToast({
                        title: res.error,
                        icon: 'none',
                        duration: 1000
                    })
                } else {
                    res.forEach(function(item) {
                        if (item.default_id == 2) {
                            that.setData({
                                address: item
                            })
                            if (wx.getStorageSync('isVipGoods') == 1) {
                                that.setData({
                                    postage: item.extra_money_goods
                                })
                            } else {
                                that.setData({
                                    postage: item.extra_money_vip
                                })
                            }
                        }
                    })
                }
            }).catch(function(e) {})
        }
    },

    goselectAddress: function() {
        wx.navigateTo({
            url: '/pages/user/address/addressList/addressList?type=select'
        })
    },

    orderMarkInput: function(e) {
        this.setData({
            orderMark: e.detail.value
        })
    },

    payChange: function(e) {
        this.setData({
            payMethods: e.detail.value
        })
    },

    goPay: function() {
        const that = this
        if (this.data.address == null) {
            wx.showToast({
                title: '请选择收货地址',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.isLoading) {
            return
        }
        wx.showLoading({
            title: '支付中'
        })
        this.setData({
            isLoading: true
        })
        wx.login({
            success(response) {
                if (response.code) {
                    //发起网络请求
                    let data = {
                        address_id: that.data.address.address_id,
                        pay_id: that.data.payMethods,
                        total_price: that.data.actualPrice,
                        breft: that.data.orderMark,
                        pay_client: 1,
                        cart_id_list: that.data.cartIdList,
                        is_goods: 1,
                        code: response.code
                    }
                    if (that.data.payMethods == 1) {
                        common.post('Order/new_order', data).then(function(response) {
                            wx.hideLoading()
                            if (response.code == 204) {
                                wx.showToast({
                                    title: response.error,
                                    icon: 'none',
                                    duration: 2000
                                })
                                that.setData({
                                    isLoading: false
                                })
                                return
                            }
                            wx.requestPayment({
                                timeStamp: response.timeStamp,
                                nonceStr: response.nonceStr,
                                package: response.package,
                                signType: response.signType,
                                paySign: response.paySign,
                                success(res) {
                                    that.setData({
                                        isLoading: false
                                    })
                                    wx.removeStorageSync('actualPrice')
                                    wx.removeStorageSync('servicePrice')
                                    wx.removeStorageSync('totalPrice')
                                    wx.removeStorageSync('cartIdList')
                                    wx.removeStorageSync('cartSelectList')
                                    wx.removeStorageSync('isVipGoods')
                                    wx.removeStorageSync('selectAddress')
                                    wx.showLoading({
                                        title: '请稍后'
                                    })
                                    setTimeout(function() {
                                        wx.hideLoading()
                                        wx.redirectTo({
                                            url: '/pages/order/orderCenter/orderCenter?orderType=2'
                                        })
                                    }, 2000)

                                },
                                fail(res) {
                                    that.setData({
                                        isLoading: false
                                    })
                                    wx.showToast({
                                        title: '支付失败',
                                        icon: 'none',
                                        duration: 1000
                                    })
                                    wx.removeStorageSync('actualPrice')
                                    wx.removeStorageSync('servicePrice')
                                    wx.removeStorageSync('totalPrice')
                                    wx.removeStorageSync('cartIdList')
                                    wx.removeStorageSync('cartSelectList')
                                    wx.removeStorageSync('isVipGoods')
                                    wx.removeStorageSync('selectAddress')
                                    wx.showLoading({
                                        title: '请稍后'
                                    })
                                    setTimeout(function() {
                                        wx.hideLoading()
                                        wx.redirectTo({
                                            url: '/pages/order/orderCenter/orderCenter?orderType=1'
                                        })
                                    }, 2000)
                                }
                            })
                        }).catch(function(e) {})
                    } else if (that.data.payMethods == 3) {
                        common.post('Order/new_order', data).then(function(response) {
                            wx.hideLoading()
                            if (response.code == 204) {
                                wx.showToast({
                                    title: response.error,
                                    icon: 'none',
                                    duration: 2000
                                })
                                that.setData({
                                    isLoading: false
                                })
                                return
                            }
                            wx.removeStorageSync('actualPrice')
                            wx.removeStorageSync('servicePrice')
                            wx.removeStorageSync('totalPrice')
                            wx.removeStorageSync('cartIdList')
                            wx.removeStorageSync('cartSelectList')
                            wx.removeStorageSync('isVipGoods')
                            wx.removeStorageSync('selectAddress')
                            wx.showModal({
                                title: '提示',
                                content: '零钱支付成功',
                                showCancel: false,
                                success(res) {
                                    if (res.confirm) {
                                        that.setData({
                                            isLoading: false
                                        })
                                        wx.showLoading({
                                            title: '请稍后'
                                        })
                                        setTimeout(function() {
                                            wx.hideLoading()
                                            wx.redirectTo({
                                                url: '/pages/order/orderCenter/orderCenter?orderType=2'
                                            })
                                        }, 2000)
                                    }
                                }
                            })
                        }).catch(function(e) {})
                    }
                }
            }
        })
    }
})